package com.yh.tea.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yh.tea.entity.User;
import com.yh.tea.model.UserQuery;
import com.yh.tea.service.UserService;
import com.yh.tea.utiity.JWTUtility;
import com.yh.tea.utiity.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yh
 * @since 2024-02-15
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //登入
    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user){
        return userService.login(user);
    }
    @PostMapping("/getUserId")
    public ResponseResult getUserID(HttpServletRequest request){
        String token=request.getHeader("token");
        return userService.getUserId(token);
    }
    @PostMapping("/getUserIdByUserName")
    public  ResponseResult getUserIdByUserName(@RequestBody User user){
        return ResponseResult.success(userService.getOne(new QueryWrapper<User>().eq("user_name",user.getUserName())).getId());
    }
    @PostMapping("/getUserList")
    public ResponseResult getUserList(@RequestBody UserQuery query){

        return userService.getUserList(query);
    }
    @PostMapping("/addUser")
    public ResponseResult addUser(@RequestBody User user){
      
        Boolean result=userService.save(user);
        if(result){
            //为其分配无权限角色，
        }
        return ResponseResult.success(result?"添加成功":"添加失败",result);
    }
    @PostMapping("/delUserList")
    public ResponseResult delUserList(@RequestBody List<Long> idList){
        Boolean result=userService.removeBatchByIds(idList);
        return ResponseResult.success(result?"删除成功":"删除失败",result);
    }

    @PostMapping("/getUserByIds")
    public ResponseResult getUserByIds(@RequestBody List<Long> userIdList){
        return userService.getUserByIds(userIdList);
    }

  }

